package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallTopic;
import com.cskaoyan.bean.CskaoyanmallTopicExample;
import com.cskaoyan.bean.bo.ExtensionTopicCreatBo;
import com.cskaoyan.bean.bo.ExtensionUpdateTopicBo;
import com.cskaoyan.bean.vo.TopicVo;
import com.cskaoyan.mapper.CskaoyanmallTopicMapper;
import com.cskaoyan.service.ExtensionTopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class ExtensionTopicServiceImpl implements ExtensionTopicService {
    @Autowired
    CskaoyanmallTopicMapper cskaoyanmallTopicMapper;


    @Override
    public TopicVo vagueQueryTopic(Integer page, Integer limit, String sort, String order, String title, String subtitle) {

        PageHelper.startPage(page, limit);

        CskaoyanmallTopicExample topicExample = new CskaoyanmallTopicExample();
        topicExample.setOrderByClause(sort + " " + order);

        CskaoyanmallTopicExample.Criteria criteria = topicExample.createCriteria();

        if (title != null && !"".equals(title)) {
            criteria.andTitleLike(title);
        }
        if (subtitle != null && !"".equals(subtitle)) {
            criteria.andSubtitleLike(subtitle);
        }

        List<CskaoyanmallTopic> topics = cskaoyanmallTopicMapper.selectByExample(topicExample);

        Iterator<CskaoyanmallTopic> iterator = topics.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().getDeleted()) {
                iterator.remove();
            }
        }

        PageInfo<CskaoyanmallTopic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();

        TopicVo topicVo = new TopicVo();
        topicVo.setTotal(total);
        topicVo.setItems(topics);

        return topicVo;
    }

    @Override
    public Integer deleteTopic(CskaoyanmallTopic topicBo) {

        topicBo.setDeleted(true);
        Integer i = cskaoyanmallTopicMapper.updateByPrimaryKey(topicBo);

        return i;
    }

    /**
     * 创建专题(添加专题)
     */
    @Override
    public CskaoyanmallTopic creatTopic(ExtensionTopicCreatBo topicBo) {

        CskaoyanmallTopic topic = new CskaoyanmallTopic();
        topic.setTitle(topicBo.getTitle());
        topic.setSubtitle(topicBo.getSubtitle());
        topic.setPrice(new BigDecimal(topicBo.getPrice()));
        topic.setReadCount(topicBo.getReadCount());
        topic.setPicUrl(topicBo.getPicUrl());
//        TODO: 返回给前端一个goods数组
//        topic.setGoods();
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        topic.setContent(topicBo.getContent());

        cskaoyanmallTopicMapper.insertSelective(topic);

        CskaoyanmallTopicExample cskaoyanmallTopicExample = new CskaoyanmallTopicExample();
        CskaoyanmallTopicExample.Criteria criteria = cskaoyanmallTopicExample.createCriteria();
        criteria.andPicUrlEqualTo(topic.getPicUrl());
        List<CskaoyanmallTopic> topics = cskaoyanmallTopicMapper.selectByExample(cskaoyanmallTopicExample);
        Integer id = topics.get(0).getId();

        topic.setId(id);

        return topic;
    }

    @Override
    public CskaoyanmallTopic updateTopic(ExtensionUpdateTopicBo topicBo) {

        String addTime = topicBo.getAddTime().replaceAll("T", " ").replaceAll("\\..*", "");
        String updateTime = topicBo.getUpdateTime().replaceAll("T", " ").replaceAll("\\..*", "");

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date addTimeNew = null;
        Date updateTimeNew = null;
        try {
            addTimeNew = sf.parse(addTime);
            updateTimeNew = sf.parse(updateTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        CskaoyanmallTopic topic = new CskaoyanmallTopic(
                // todo:
//                topicBo.getId(),
//                topicBo.getTitle(),
//                topicBo.getSubtitle(),
//                topicBo.getContent(),
//                new BigDecimal(topicBo.getPrice()),
//                topicBo.getReadCount(),
//                topicBo.getPicUrl(),
//                topicBo.getSortOrder(),
//                topicBo.getGoods(),
//                addTimeNew,
//                updateTimeNew,
//                topicBo.getDeleted()
        );

        cskaoyanmallTopicMapper.updateByPrimaryKey(topic);


        return topic;
    }
}
